// pages/hospital/hospital.js
//const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    longitude: 0,
    latitude: 0,
    scale: 18,
  },
  pois: [],
  skip: 1,

  getLocation: function (skip) {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        var longitude = res.longitude
        var latitude = res.latitude
        wx.request({
          url: 'https://restapi.amap.com/v3/place/around?',
          data: {
            key: '4ab30d5a3b5378015252a449c00384bc',
            keywords: '猫|狗|宠物|宠物医院',
            location: res.longitude + ',' + res.latitude,
            page: skip,
            radius:50000,
            type: '090700|090701|090702'
          },
          success: (res) => {
            this.pois = this.pois.concat(res.data.pois)
            this.setData({
              markers: this.pois,
              latitude: latitude,
              longitude: longitude
            })
          }
        })
      },
      // 用户总是不愿意授权的话
      fail(res) {
        wx.navigateBack({})
      }
    })
  },
  copy: function (e) {
    var code = e.currentTarget.dataset.copy;
    wx.setClipboardData({
      data: code,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '复制失败',
        });
      }
    })
  },

  authorize: function () {
    const that = this
    wx.authorize({
      scope: 'scope.userLocation',
      success(res) {
        console.log(res)
        that.getLocation(that.skip)
      },
      fail(res) {
        console.log(res)
        wx.showModal({
          title: '主人，不知道你住哪里……',
          content: '(ノへ`、)请问愿意告诉小萌，主人的定位吗？不愿意的话，不知道怎么给主人推荐周边宠物医院哦',
          showCancel: "true",
          cancleText: "就不告诉你",
          confirmText: '可以呀',
          success(res) {
            if (res.confirm) {
              // 微信不支持二次直接弹窗发起授权（否则会一直fail），所以弹窗授权失败后，只能调opensetting去官方给的页面里设置
              wx.openSetting({
                success(res) {
                  if (res.authSetting.userLocation) {
                    that.getLocation(that.skip)
                  }
                  else {
                    wx.navigateBack({})
                  }
                }
              })
            }
            else if (res.cancel) {
              wx.navigateBack({})
            }
          }
        })
      }
    })
  },

  onLoad: function (options) {
    var that = this
    var pois = Array()
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          that.authorize()
        }
        else {
          that.getLocation(that.skip)
        }
      }
    })

  },
  onReachBottom: function () {
    wx.showToast({
      title: '正在为主人找更多的宠物医院',
      icon: "none"
    })
    this.skip += 1
    this.getLocation(this.skip)
  },
  goWhere: function (e) {
    var longitude = e.currentTarget.dataset.location.split(',')[0]
    var latitude = e.currentTarget.dataset.location.split(',')[1]
    this.setData({
      latitude: parseFloat(latitude),
      longitude: parseFloat(longitude)
    })
  }
})